/*
 ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1

 * The contents of this file are subject to the Mozilla Public License Version 
 * 1.1 (the "License"); you may not use this file except in compliance with 
 * the License. You may obtain a copy of the License at 
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Blazing Games Inc. (Billy D. Spelchan) code.
 *
 * The Initial Developer of the Original Code is
 * Blazing Games Inc.
 * Portions created by the Initial Developer are Copyright (C) 1999, 2003, 2005-2006
 * the Initial Developer. All Rights Reserved.
 * 
 * Contributor(s):
 *
 ***** END LICENSE BLOCK *****
 */
package com.blazinggames.holeman;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.net.MalformedURLException;
//import java.util.*;

//import com.blazinggames.urcl.data.LoadingBar;

public class HM
extends Applet
implements ActionListener, Runnable
{
// *** CONSTANTS ***
  // screen modes
  private static final int SCREEN_GAME = 1;
  private static final int SCREEN_TITLE = 2;
  private static final int SCREEN_SPEED = 3;
  private static final int SCREEN_TRAILER = 4;
  private static final int SCREEN_MSG_DIE = 5;
  private static final int SCREEN_MSG_RESET = 6;
  private static final int SCREEN_MSG_WIN = 7;
  private static final int SCREEN_MSG_SETWIN = 8;
  private static final int SCREEN_MSG_GAMEOVER = 9;

  private static boolean INJAR = false;

// *** VARIABLES ***
  Map map;
  TitleScreen titleScreen;
  SpeedSelect speedScreen;
  GameScreen gameScreen;
  Messages msgScreen;

  int nStartLives;

  AudioClip auHole, auWin, auDie, auCrystal, auMonster;
  AudioClip auSilence;
  
	HMLoader _loader;
	boolean _finishedLoading = false;
  	Thread _runner;
  	
	/**
     The constructor class.  Adjusts the windows size and title, creates
     the menu bar, then enables the close window event.
	*/
	public HM() {
	}

	public void init() 
	{
		if ( ! _finishedLoading)
		{
			_loader = new HMLoader(this);
			setLayout(new CardLayout());
			add("loader", _loader);
			((CardLayout)getLayout()).show(this, "loader");
			loadGame();
		}
  	}
	public void run() 
	{
//		_parent.loadGame();

		do
		{
			repaint(50);
			try{Thread.sleep(50);}catch(InterruptedException ie){;}
		}
		while (_finishedLoading == false);
		System.out.println("Thread Exiting");
	}

	public void loadGame()
	{
		map = new Map();
		_loader.setPercent(10);
		titleScreen = new TitleScreen(map, this, this, INJAR);
		_loader.setPercent(30);
		gameScreen = new GameScreen(map, this, getClass(), getCodeBase(), INJAR);
		//    gameScreen.screenLow();
		_loader.setPercent(70);
		System.out.println("Game screen loaded");
		speedScreen = new SpeedSelect(this);
		_loader.setPercent(80);
		msgScreen = new Messages(map, this);
		_loader.setPercent(90);
		try {Thread.sleep(10000);} catch (InterruptedException ignore){;}
		
		add("title", titleScreen);
		add("speed", speedScreen);
		add("msg", msgScreen);
		add("game", gameScreen);
		if (!parseParams()) 
		{
			msgScreen.setMessage(Messages.MSG_INVALIDMAP);
			((CardLayout)getLayout()).show(this, "msg");
		}
		else {
			auSilence = getAudioClip(getDocumentBase(), "res/sound/silence.au");
			auSilence.loop();
			auHole = getAudioClip(getDocumentBase(), "res/sound/hm_hole.au");
			auWin = getAudioClip(getDocumentBase(), "res/sound/hm_win.au");
	  		auDie = getAudioClip(getDocumentBase(), "res/sound/hm_die.au");
			auCrystal = getAudioClip(getDocumentBase(), "res/sound/hm_cryst.au");
			auMonster = getAudioClip(getDocumentBase(), "res/sound/hm_monst.au");

			map.setSoundClips(auHole, auWin, auDie, auCrystal, auMonster);
			map.setSound(true);
		}
		_finishedLoading = true;
		repaint();
		((CardLayout)getLayout()).show(this, "title");
	}
/*
	public void paint(Graphics g) 
	{
		if ( ! _finishedLoading )
			_loader.render(g, this);
		else
			super.paint(g);
	}
*/	
  boolean parseParams() {
    String sTemp;

    sTemp = getParameter("lives");
    if (sTemp == null)
      nStartLives = 3;
    else
      nStartLives = Integer.parseInt(sTemp);

    sTemp = getParameter("map");
    if (sTemp == null)
      return false;
    else {
      try {
        if (!map.loadSet(new URL(getDocumentBase(), sTemp)))
          return false;
      } catch (MalformedURLException mue) {
        System.out.println("INVALID URL");
        return false;
      }
    }

    map.startLevel(0);
    gameScreen.setTest(false);
    map.player.nLives = nStartLives;

    return true;
  }

  private void changeScreen(int nID) {
    System.gc();
    switch (nID) {
      case SCREEN_TITLE:
        map.startLevel(0);
        gameScreen.setTest(false);
        map.player.nLives = nStartLives;
        ((CardLayout)getLayout()).show(this, "title");
          titleScreen.requestFocus();
        break;
      case SCREEN_GAME:
        map.setDelay(speedScreen.getSpeed());
        ((CardLayout)getLayout()).show(this, "game");
          gameScreen.requestFocus();
        break;
      case SCREEN_SPEED:
        ((CardLayout)getLayout()).show(this, "speed");
          speedScreen.requestFocus();
        break;
      case SCREEN_MSG_DIE:
        msgScreen.setMessage(Messages.MSG_DIE);
        ((CardLayout)getLayout()).show(this, "msg");
          msgScreen.requestFocus();
        break;
      case SCREEN_MSG_RESET:
        msgScreen.setMessage(Messages.MSG_RESET);
        ((CardLayout)getLayout()).show(this, "msg");
          msgScreen.requestFocus();
        break;
      case SCREEN_MSG_WIN:
        msgScreen.setMessage(Messages.MSG_WIN);
        ((CardLayout)getLayout()).show(this, "msg");
          msgScreen.requestFocus();
        break;
      case SCREEN_MSG_SETWIN:
        msgScreen.setMessage(Messages.MSG_SETWIN);
        ((CardLayout)getLayout()).show(this, "msg");
          msgScreen.requestFocus();
        break;
      case SCREEN_MSG_GAMEOVER:
        msgScreen.setMessage(Messages.MSG_GAMEOVER);
        ((CardLayout)getLayout()).show(this, "msg");
          msgScreen.requestFocus();
        break;
      case SCREEN_TRAILER:
        if (map.nCurLevel < 8)
          msgScreen.setMessage(Messages.MSG_TRAILER);
        else
          msgScreen.setMessage(Messages.MSG_SETWIN);
        ((CardLayout)getLayout()).show(this, "msg");
          msgScreen.requestFocus();
        break;
    }
  }

  /**
    Handle Panel actions.
  */
	public void actionPerformed(ActionEvent e) {
		String sID = e.getActionCommand();

    if (sID.equals("Next") )
      changeScreen(SCREEN_TRAILER);
    else if (sID.equals("Play") )
      changeScreen(SCREEN_GAME);
    else if (sID.equals("Win"))
      changeScreen(SCREEN_MSG_WIN);
    else if (sID.equals("Killed"))
      changeScreen(SCREEN_MSG_DIE);
    else if (sID.equals("Reset"))
      changeScreen(SCREEN_MSG_RESET);
    else if (sID.equals("Speed"))
      changeScreen(SCREEN_SPEED);
    else if (sID.equals("GameOver"))
      changeScreen(SCREEN_MSG_GAMEOVER);
    else if (sID.equals("Title"))
      changeScreen(SCREEN_TITLE);
    else
      System.out.println("Unknown command: " + sID);
  }

}
